package com.rj.bd.sys.manage.user;
/**
 * @desc User模块的C层
 * @author 郭瑞博
 * @date 2024-12-26 11:00
 **/


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rj.bd.common.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /***
     * @desc 1.查询全部（分页+条件查询）
     * @param pageNum  当前页
     * @param pageSize 每页显示多少条
     * @param username 条件查询之用户名
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Map<String, Object> query(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "2") int pageSize,
            @RequestParam(required = false) String username
    ) {
        User user = new User();
        user.setUsername(username);
        System.err.println("UserController -> query");//必须要写，为了调试方便
        Map<String, Object> map = new HashMap<>();
        IPage<User> page = userService.queryUserPage(pageNum, pageSize, user);
        map.put("code", 200);
        map.put("msg", "查询用户信息成功");
        map.put("data", page);
        return map;
    }

    /**
     * @param user 待添加的对象
     * @return Map
     * @desc 2.添加用户
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody User user) {
        System.err.println("UserController -> add");//必须要写，为了调试方便
        Map<String, Object> map = new HashMap<>();
        String result = userService.addUser(user);
        map.put("code", 200);
        map.put("msg", "添加用户成功");
        map.put("data", result);
        return map;
    }

    /**
     * @param user 待修改的对象
     * @return Map
     * @desc 3.修改用户
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Map<String, Object> update(@RequestBody User user) {
        System.err.println("UserController -> update");//必须要写，为了调试方便
        Map<String, Object> map = new HashMap<>();
        String result = userService.updateUser(user);
        map.put("code", 200);
        map.put("msg", "修改用户成功");
        map.put("data", result);
        return map;

    }

    /**
     * @param id 待删除的本条记录id
     * @return Map
     * @desc 4.通过id删除用户
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Map<String, Object> delete(Integer id) {
        System.err.println("UserController -> delete");//必须要写，为了调试方便
        Map<String, Object> map = new HashMap<>();
        String result = userService.deleteUser(id);
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        map.put("data", result);
        return map;
    }
}
